package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.annotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({ "all" })
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户ID查询用户/主播基本信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户ID查询用户/主播基本信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    /**
     * 获取用户声音列表付费情况
     *
     * @param userId                    用户ID
     * @param albumId                   专辑ID
     * @param needCheckBuyStateTrackIds 需要检查购买状态的声音ID列表
     * @return 声音ID与付费状态的映射，1表示已购买，0表示未购买
     */
    @Operation(summary = "获取用户声音列表付费情况")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(
            @PathVariable Long userId,
            @PathVariable Long albumId,
            @RequestBody List<Long> needCheckBuyStateTrackIds) {
        Map<Long, Integer> paidStateMap = userInfoService.getUserTrackPaidState(userId, albumId,
                needCheckBuyStateTrackIds);
        return Result.ok(paidStateMap);
    }

    /**
     * 判断用户是否购买过专辑
     *
     * @param albumId 专辑ID
     * @return true-已购买，false-未购买
     */
    @NeedLogin
    @Operation(summary = "判断用户是否购买过专辑")
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Boolean isPaid = userInfoService.isPaidAlbum(albumId);
        return Result.ok(isPaid);
    }

    /**
     * 判断用户是否购买过声音列表
     *
     * @param trackIdList 声音ID列表
     * @return true-至少购买过一个声音，false-没有购买过任何声音
     */
    @NeedLogin
    @Operation(summary = "判断用户是否购买过声音列表")
    @PostMapping("/userInfo/isPaidTrack")
    public Result<Boolean> isPaidTrack(@RequestBody List<Long> trackIdList) {
        Boolean result = userInfoService.isPaidTrack(trackIdList);
        return Result.ok(result);
    }

    /**
     * 保存用户购买记录
     *
     * @param userPaidRecordVo 用户购买记录VO
     * @return 成功或失败
     */
    @Operation(summary = "保存用户购买记录")
    @PostMapping("/userInfo/savePaidRecord")
    public Result<Void> savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }

}
